from channels.generic.websocket import WebsocketConsumer

# Create your views here.


class Message(WebsocketConsumer):
    def __init__(self, *args, **kwargs):
        super(Message, self).__init__(*args, **kwargs)
        self.connectStatus = False

    # websocket建立连接时执行方法
    def connect(self):
        self.accept()

    # websocket断开时执行方法
    def disconnect(self, close_code):
        self.close()

    # websocket发送消息的重写方法
    def send(self, text_data=None, bytes_data=None, close=False):
        if self.connectStatus:
            super(Message, self).send(text_data=text_data, bytes_data=bytes_data, close=close)

    # 从websocket接收到消息时执行函数
    def receive(self, text_data=None, bytes_data=None):
        pass
        # self.send(text_data=json.dumps({"type": data_type, "message": message}, ensure_ascii=False))
